package com.codewerkz.util;

import com.codewerkz.prompty.PresentationList;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * A sample ListAdapter that presents content from arrays of speeches and
 * text.
 * 
 */
public class TwoLineListAdapter extends BaseAdapter {
    
    public TwoLineListAdapter(Context context, String[] titles, String[] descriptions) {
        mContext = context;
        mContext.getResources();
        
        mTitles = titles;
        mDescriptions = descriptions;
    }

    /**
     * The number of items in the list is determined by the number of speeches
     * in our array.
     * 
     * @see android.widget.ListAdapter#getCount()
     */
    public int getCount() {
        return mTitles.length;
    }

    /**
     * Since the data comes from an array, just returning the index is
     * Sufficient to get at the data. If we were using a more complex data
     * structure, we would return whatever object represents one row in the
     * list.
     * 
     * @see android.widget.ListAdapter#getItem(int)
     */
    public Object getItem(int position) {
        return position;
    }

    /**
     * Use the array index as a unique id.
     * 
     * @see android.widget.ListAdapter#getItemId(int)
     */
    public long getItemId(int position) {
        return position;
    }

    /**
     * Make a SpeechView to hold each row.
     * 
     * @see android.widget.ListAdapter#getView(int, android.view.View,
     *      android.view.ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup parent) {
    	TwoLineListView sv;
        if (convertView == null) {
            sv = new TwoLineListView(mContext, mTitles[position],
            		mDescriptions[position]);
            sv.setId(position);
        } else {
            sv = (TwoLineListView) convertView;
            sv.setTitle(mTitles[position]);
            sv.setDescription(mDescriptions[position]);
        }

        return sv;
    }
    
    /**
     * We will use a SpeechView to display each speech. It's just a LinearLayout
     * with two text fields.
     *
     */
    public static class TwoLineListView extends LinearLayout {
        public TwoLineListView(Context context, String title, String description) {
            super(context);

            this.setOrientation(VERTICAL);

            // Here we build the child views in code. They could also have
            // been specified in an XML file.

            mTitle = new TextView(context);
            mTitle.setText(title);
            mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) (PresentationList.sFontSizePx * 1.5));
            mTitle.setTextColor(Color.WHITE);
            mTitle.setId(1);
            addView(mTitle, new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            mDescription = new TextView(context);
            mDescription.setText(description);
            mDescription.setId(2);
            mDescription.setPadding(0, 5, 0, 5);
            mDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, PresentationList.sFontSizePx);
            addView(mDescription, new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }

        /**
         * Convenience method to set the title of a SpeechView
         */
        public void setTitle(String title) {
            mTitle.setText(title);
        }

        /**
         * Convenience method to set the dialogue of a SpeechView
         */
        public void setDescription(String words) {
        	mDescription.setText(words);
        }

        private TextView mTitle;
        private TextView mDescription;
    }

    /**
     * Remember our context so we can use it when constructing views.
     */
    private Context mContext;
    private String[] mTitles;
    private String[] mDescriptions;
   
}
